#include "windowhandler.h"

WindowMessageHandler::WindowMessageHandler()
{
	//ctor
}

WindowMessageHandler::~WindowMessageHandler()
{
	//dtor
}

	WNDPROC old;

LRESULT CALLBACK WindowMessageHandler::gg
	(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
		HDC hdc;
	HDC hdcmem;	// double buffering
	HBITMAP hbmMem, hbmMemOld;
	RECT rect;

	PAINTSTRUCT ps;
	static int x=100;
	static int y=100;
	static bool s=false;

	HFONT font, oldfont;
	LPCTSTR str= _T("Hello Roguelike55");
	LPCTSTR stat= _T("A");

    switch(message) {

	case WM_KEYDOWN:
		switch(wParam) {
		case VK_LEFT:
			x-=16;
			break;
		case VK_RIGHT:
			x+=16;
			break;
		case VK_UP:
			y-=16;
			break;
		case VK_DOWN:
			y+=16;
			break;
		}
		InvalidateRect(hWnd,NULL,FALSE);//TRUE 하면 지우고 다시그리고... FALSE 는 지우지않고 그린다
		return 0;
	case WM_CHAR:
		switch(wParam) {
			case 'C':
				s=s^1;
				break;
			case 'V':
				old = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WindowMessageHandler::gx);
				break;
		}
		break;
	case WM_PAINT:
		hdc=BeginPaint(hWnd,&ps);

		GetClientRect(hWnd, &rect);


		hdcmem = CreateCompatibleDC(hdc); // double buffering
		hbmMem = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);//3
		hbmMemOld =(HBITMAP)SelectObject(hdcmem, hbmMem);//

		// 화면을 모두 까만색으로 채운다.
		BitBlt(hdcmem, 0, 0, rect.right, rect.bottom, 0, 0, 0, BLACKNESS);
		SetBkMode(hdcmem, TRANSPARENT);

		font=CreateFont(8,16,0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                PROOF_QUALITY, FF_MODERN, _T("pc8x16"));
				oldfont=(HFONT)SelectObject(hdcmem,font);

		// 글자 색깔은 여기서 정해요
		SetTextColor(hdcmem, 0xFF00FF);

		TextOut(hdcmem,x,y,_T("@"),1);
		if( s )
			TextOut(hdcmem,420,420,stat,_tcslen(stat));

		TextOut(hdcmem, 50,50,str,_tcslen(str));

		SelectObject(hdcmem,oldfont);
		DeleteObject(font);



		BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdcmem, 0, 0, SRCCOPY);

		SelectObject(hdcmem, hbmMemOld); //-4
		DeleteObject(hbmMem); //-3
		DeleteDC(hdcmem); //-2
		//ValidateRect(hWnd, NULL);

		EndPaint(hWnd,&ps);



		return 0;
	case WM_DESTROY:
		SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)old);
		PostQuitMessage(0);
		return 0;
	}
	//CallWindowProc(old, hWnd, message, wParam, lParam);
	return(DefWindowProc(hWnd,message,wParam,lParam));
}

LRESULT CALLBACK WindowMessageHandler::gx
	(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int x=16;
	static int y=16;
	static bool s=false;

    switch(message) {
	case WM_KEYDOWN:
		switch(wParam) {
		case VK_LEFT:
			x-=16;
			break;
		case VK_RIGHT:
			x+=16;
			break;
		case VK_UP:
			y-=16;
			break;
		case VK_DOWN:
			y+=16;
			break;
		}
		InvalidateRect(hWnd,NULL,FALSE);//TRUE 하면 지우고 다시그리고... FALSE 는 지우지않고 그린다
		return 0;
	case WM_CHAR:
		switch(wParam) {
			case 'C':
				s=s^1;
				break;
			case 'V':
				old = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WindowMessageHandler::gg);
				break;
		}
		break;
	case WM_PAINT:

		//wDraw->Draw(&hWnd);


		return 0;
	case WM_DESTROY:
		SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)old);
		PostQuitMessage(0);
		return 0;
	}
	return(DefWindowProc(hWnd,message,wParam,lParam));
}

